﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBLClient.DBLNoManageService;
using DBLClient;
namespace DBLClient.DBLForm
{
    public partial class DBLNoLast3CodeForm : Form
    {
        public DBLNoLast3CodeForm()
        {
            InitializeComponent();
        }

        private void queryButton_Click(object sender, EventArgs e)
        {
            ManageDBLNoService dblService = new ManageDBLNoService();
            string strWhere = queryTextBox.Text;
            if(strWhere.Equals(""))
            {
                MessageBox.Show("请输入查询子串");
                queryTextBox.Focus();
                return;
            }
            DataSet ds = dblService.GetLast3Code(strWhere);
            
            dataGridViewQuery.DataSource = ds.Tables[0];
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            //把左侧选中的行的值放入右侧listbox中
            string s = string.Empty;
            if (dataGridViewQuery.SelectedRows.Count != 0)
            {
               s = dataGridViewQuery.CurrentRow.Cells[0].Value.ToString();
            }
            if (s.Equals(""))
            {
                MessageBox.Show("请选中一行");
                return;
            }
            listBoxEdit.Items.Add(s);
        }

        private void buttonPutIn_Click(object sender, EventArgs e)
        {
            //把textboxEdit的值放入编辑区
            string strEdit = textBoxEdit.Text;
            if (strEdit.Equals(""))
            {
                MessageBox.Show("请输入要放入编辑区的流水号");
                textBoxEdit.Focus();
                return;
            }
            listBoxEdit.Items.Add(strEdit);
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            //把listbox中选中的值移出
            if (listBoxEdit.SelectedItem == null)
            {
                MessageBox.Show("请选择要从编辑区删除的条目");
                return;
            }
            string s = listBoxEdit.SelectedItem.ToString();
            if (s.Equals(""))
            {
                MessageBox.Show("请选择要从编辑区删除的条目");
                return;
            }
            listBoxEdit.Items.Remove(s);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            //清空编辑区
            
            listBoxEdit.Items.Clear();
        }

        private void buttonGenerateRepeat_Click(object sender, EventArgs e)
        {
            //生成重复号放入listboxEdit中
            ManageDBLNoService dblnoService = new ManageDBLNoService();
            string [] repeatArray = dblnoService.GenerateRepeatCode(3);
            foreach(string s in repeatArray)
            {
                listBoxEdit.Items.Add(s);
            }
        }

        private void buttonGenerateSequence_Click(object sender, EventArgs e)
        {
            //生成连号放入listboxEdit中
            ManageDBLNoService dblnoService = new ManageDBLNoService();
            string[] sequenceArray = dblnoService.GenerateSequenceCode(3, true);
            foreach (string s in sequenceArray)
            {
                listBoxEdit.Items.Add(s);
            }
        }




        private void buttonAvailable_Click(object sender, EventArgs e)
        {
            //把listbox里面的所有内容设为可以开放注册的，即为“A”
            if (MessageBox.Show("确定要把编辑列表中的号码开放注册吗？", "确定要把编辑列表中的号码开放注册吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)  
            {
               
                List<string> list = new List<string>();
                foreach(string item in listBoxEdit.Items)
                {
                    list.Add(item.ToString());
                }

                string [] array = list.ToArray();
                ManageDBLNoService dblnoService = new ManageDBLNoService();
                dblnoService.UpdateLast3Code(array,true);
            }  
        }

        private void buttonReserve_Click(object sender, EventArgs e)
        {
            //把listbox里面的所有内容设为可以开放注册的，即为“A”
            if (MessageBox.Show("确定要把编辑列表中的号码预留吗？", "确定要把编辑列表中的号码预留吗？", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                List<string> list = new List<string>();
                foreach (string item in listBoxEdit.Items)
                {
                    list.Add(item);
                }

                string[] array = list.ToArray();
                ManageDBLNoService dblnoService = new ManageDBLNoService();
                dblnoService.UpdateLast3Code(array, false);
            }
 
        }

        private void buttonQueryReserve_Click(object sender, EventArgs e)
        {
            ManageDBLNoService service = new ManageDBLNoService();
            DataSet ds = service.GetAllLast3CodeReserve();
            dataGridViewQuery.DataSource = ds.Tables[0];
        }

        

    }
}
